﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Game.Foundation;

namespace Game.Client
{
    public class RemoteServer : MarshalByRefObject, IMediator
    {
        public static RemoteServer GetSingletomServer(int port)
        {
            return (RemoteServer)Activator.GetObject(typeof(RemoteServer), string.Format("tcp://localhost:{0}/testuri", port));
            //return (RemoteServer)Activator.GetObject(typeof(RemoteServer), "ipc://abcd/testuri");
        }

        private Dictionary<string, IParentClient> _parentDict;

        private RemoteServer()
        {
            this._parentDict = new Dictionary<string, IParentClient>();
        }

        private bool ContainsParent(string key)
        {
            bool result = this._parentDict.ContainsKey(key);
            if (result)
            {
                result = this._parentDict[key] != null;
            }

            return result;
        }

        private bool ContainsChild(string key)
        {
            bool result = this.ContainsParent(key);
            if (result)
            {
                result = this._parentDict[key].ChildClient != null;
            }

            return result;
        }

        #region IMediator 成员

        public void RegisterParent(string key, IParentClient parent)
        {
            this._parentDict[key] = parent;
        }

        public void UnRegisterParent(string key)
        {
            if (this._parentDict.ContainsKey(key))
            {
                this._parentDict.Remove(key);
            }
        }

        public void RegisterChild(string key, IChildClient child)
        {
            if (this.ContainsParent(key))
            {
                this._parentDict[key].ChildClient = child;
            }
        }

        public void UnRegisterChild(string key)
        {
            if (this.ContainsChild(key))
            {
                this._parentDict[key].ChildClient = null;
            }
        }

        public void RequestChangeStatus(string key, Game.Foundation.GameStatus target)
        {
            if (this.ContainsParent(key))
            {
                this._parentDict[key].ChangeReadyStatus(target);
            }
        }

        public void RequestDoOperation(string key, byte[] msg)
        {
            if (this.ContainsParent(key))
            {
                this._parentDict[key].DoGameOperation(msg);
            }
        }

        public void RequestSendMessage(string key, string msg)
        {
            if (this.ContainsParent(key))
            {
                this._parentDict[key].SendChatMessage(msg);
            }
        }

        public void InitializeGame(string key,byte[] buffer)
        {
            if (this.ContainsChild(key))
            {
                this._parentDict[key].ChildClient.Initialize(buffer);
            }
        }

        public void ExchangeHand(string key, int seatIndex)
        {
            if (this.ContainsChild(key))
            {
                this._parentDict[key].ChildClient.ExchangeHand(seatIndex);
            }
        }

        public void GameSynchronize(string key, byte[] msg)
        {
            if (this.ContainsChild(key))
            {
                this._parentDict[key].ChildClient.GameSynchronize(msg);
            }
        }

        public void GameOver(string key, byte[] msg)
        {
            if (this.ContainsChild(key))
            {
                this._parentDict[key].ChildClient.GameOver(msg);
            }
        }

        #endregion
    }
}
